package com.android.contact.delete;

import java.util.Vector;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class main extends Activity {
	String TAG = "Delete2.0";
	ContentResolver cr = null;
    Activity m_activity = this;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cr = getContentResolver();
        Button button = null;
        button = (Button)findViewById(R.id.Button01);//delete button
        if(button != null){
        	button.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox checkbox = null;
					checkbox = (CheckBox)findViewById(R.id.CheckBox01);//contact checkbox
					if(checkbox != null){
						if(checkbox.isChecked()){
							deleteAllContact();
						}
					}
					checkbox = (CheckBox)findViewById(R.id.CheckBox02);//calendar checkbox
					if(checkbox != null){
						if(checkbox.isChecked()){
							deleteAllCalendar();
						}
					}
					checkbox = (CheckBox)findViewById(R.id.CheckBox03);//sms checkbox
					if(checkbox != null){
						if(checkbox.isChecked()){
							deleteAllSms();
						}
					}
				}
        	}
        	);
        }
        button = (Button)findViewById(R.id.Button02);//exit button
        if(button != null){
        	button.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					m_activity.finish();
				}
        	}
        	);
        }
        Log.i(TAG, "Out onCreate()");
    }

    void deleteAllContact(){
    	Log.i(TAG, "In deleteAllContact()");
    	Uri uri_contacts = ContactsContract.Contacts.CONTENT_URI;
    	String str_column_name = ContactsContract.Contacts._ID;
    	String[] projection = {str_column_name};
    	int columnIndex = 0;
    	String str_id = "";
    	Vector<String> vector_id = new Vector<String>();
    	int delRow = 0;
    	String where = "";
    	try {
    		Cursor cursor = cr.query(uri_contacts, projection, null, null, null);
    		if(cursor.moveToFirst()){
    			do{
    				columnIndex = cursor.getColumnIndex(str_column_name);
    				str_id = cursor.getString(columnIndex);
    				vector_id.add(str_id);
    			}while(cursor.moveToNext());
    		}
    		cursor.close();
    		for(int i=0; i<vector_id.size(); i++){
    			str_id = vector_id.get(i);
    			where = str_column_name+"="+str_id;
    			delRow = cr.delete(uri_contacts, where, null);
    			Log.i(TAG, "deleteAllContact(),delRow:"+delRow);
    		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "deleteAllContact(),Exception");
			e.printStackTrace();
		}
		Log.i(TAG, "Out deleteAllContact()");
    }
    
    void deleteAllCalendar(){
    	Log.i(TAG, "In deleteAllCalendar()");
    	String strUriEvents = "content://calendar/events";
    	Uri uri_calendar = Uri.parse(strUriEvents);
    	String str_column_name = "_id";
    	String[] projection = {str_column_name};
    	int columnIndex = 0;
    	String str_id = "";
    	Vector<String> vector_id = new Vector<String>();
    	int delRow = 0;
    	String where = "";
    	try {
    		Cursor cursor = cr.query(uri_calendar, projection, null, null, null);
    		if(cursor.moveToFirst()){
    			do{
    				columnIndex = cursor.getColumnIndex(str_column_name);
    				str_id = cursor.getString(columnIndex);
    				vector_id.add(str_id);
    			}while(cursor.moveToNext());
    		}
    		cursor.close();
    		for(int i=0; i<vector_id.size(); i++){
    			str_id = vector_id.get(i);
    			where = str_column_name+"="+str_id;
    			delRow = cr.delete(uri_calendar, where, null);
    			Log.i(TAG, "deleteAllCalendar(),delRow:"+delRow);
    		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "deleteAllCalendar(),Exception");
			e.printStackTrace();
		}
		Log.i(TAG, "Out deleteAllCalendar()");
    }

    void deleteAllSms(){
    	Log.i(TAG, "In deleteAllSms()");
    	Uri uri_sms = Uri.parse("content://sms");;
    	String str_column_name = "_id";
    	String[] projection = {str_column_name};
    	int columnIndex = 0;
    	String str_id = "";
    	Vector<String> vector_id = new Vector<String>();
    	int delRow = 0;
    	String where = "";
    	try {
    		Cursor cursor = cr.query(uri_sms, projection, null, null, null);
    		if(cursor.moveToFirst()){
    			do{
    				columnIndex = cursor.getColumnIndex(str_column_name);
    				str_id = cursor.getString(columnIndex);
    				vector_id.add(str_id);
    			}while(cursor.moveToNext());
    		}
    		cursor.close();
    		for(int i=0; i<vector_id.size(); i++){
    			str_id = vector_id.get(i);
    			where = str_column_name+"="+str_id;
    			delRow = cr.delete(uri_sms, where, null);
    			Log.i(TAG, "deleteAllSms(),delRow:"+delRow);
    		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "deleteAllSms(),Exception");
			e.printStackTrace();
		}
		Log.i(TAG, "Out deleteAllSms()");
    }
}